﻿using Microsoft.AspNetCore.Mvc;
using Model;
using MyMusicList.DBModel;
using MyMusicList.DBModelValidate;
using MyMusicList.model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MyMusicList.BLL
{
    public class User:UserModelRule
    {
        private DataDBContext _context;
        //S_BLL sb = null;

        public User()
        {

        }

        public User(DataDBContext context)
        {
            _context = context;
            //sb = new S_BLL(_context);
        }

        public object Get()
        {
            S_Result.data = _context.User.ToList();
            if (S_Result.data == null)
            {
                S_Result.error = "暂无此数据";
                return false;
            }


            return S_Result.data;
        }

        public object GetId(int id)
        {
            S_Result.data = _context.User.Where(x => x.User_Id == id).FirstOrDefault();
            if (S_Result.data == null)
            {
                S_Result.error = "暂无此数据";
                return false;

            }
            return S_Result.data;
        }


        public object post(DBModel.User value)
        {
            if (!getIsOK<DBModel.User>(value))
            {
                return false;
            }

            _context.User.Add(value);
            int a = _context.SaveChanges();

            if (a > 0)
            {
                S_Result.msg = "成功";
                return true;
            }
            else
            {
                S_Result.msg = "添加失败";
                return false;
            }
        }

        public object put(int id, DBModel.User value)
        {
            var list = _context.User.Where(x => x.User_Id == id).FirstOrDefault();
            if (list == null)
            {
                S_Result.error = "暂无此数据";
                return false;

            }

            if (!getIsOK<DBModel.User>(value))
            {
                return false;
            }
            _context.User.Update(value);
            int a = _context.SaveChanges();

            if (a > 0)
            {
                S_Result.msg = "成功";
                return true;
            }
            else
            {
                S_Result.msg = "添加失败";
                return false;
            }
        }

        public object Delete(int id)
        {
            var list = _context.User.Where(x => x.User_Id == id).FirstOrDefault();
            if (list == null)
            {
                S_Result.error = "暂无此数据";
                return false;

            }
            _context.User.Remove(list);
            int aa = _context.SaveChanges();

            if (aa > 0)
            {
                S_Result.msg = "删除成功";
                return true;
            }
            else
            {
                S_Result.msg = "删除失败";
                return false;
            }
        }

        public object login(DBModel.User value)
        {
            if (!getIsOK<DBModel.User>(value))
            {
                return false;
            }

            var userInfo = _context.User.Where(x=>x.User_Name== value.User_Name && x.Password== value.Password).FirstOrDefault();

            if (userInfo == null)
            {
                S_Result.error = "暂无此数据";
                return false;
            }

            Login login = new Login();
            login.AuthKey = "aaaaa";
            login.SessionId = "wwww";


            S_Result.data = login;

            return S_Result.data;
        }
    }
}
